package org.cary.utils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtils {

	private static Pattern numpattern = Pattern.compile("\\-?[0-9]*\\.?[0-9]*");

	/**
	 * 获取指定编号的直接下级,并从小到大排序
	 * 
	 * @return
	 */
	public static List<String> getDirectChildren(String curTreeNo, List<String> all) {
		List<String> list = new ArrayList<String>();

		for (String treeNo : all) {
			String rex = "^" + curTreeNo + "[0-9]{2}$";
			Pattern p = Pattern.compile(rex);
			Matcher m = p.matcher(treeNo);
			if (m.matches()) {
				list.add(treeNo.substring(curTreeNo.length(), treeNo.length()));
			}
		}
		Collections.sort(list, new Comparator<Object>() {
			public int compare(Object o1, Object o2) {
				Integer a = Integer.valueOf((String) o1);
				Integer b = Integer.valueOf((String) o2);
				return a - b;
			}
		});
		return list;
	}

	public static String fill(int no) {
		String res = Integer.valueOf(no).toString();
		if (res.length() == 2)
			return res;
		if (res.length() == 1)
			return "0" + res;
		return null;
	}

	public static boolean isBlank(String text) {
		if (text == null || text.trim().length() < 1)
			return true;
		return false;
	}

	public static boolean isNotBlank(String text) {
		return !isBlank(text);
	}

	/**
	 * 得到定长的字符
	 * 
	 * @param s
	 * @return
	 * @author wxy 2015年6月8日星期一中国标准时间上午9:58:09
	 */
	public static String getSpecificString(String s, int len) {
		if (StringUtils.isBlank(s)) {
			return "";
		}
		if (s.length() < len) {
			return s;
		} else {
			return s.substring(0, len);
		}
	}

	/**
	 * 验证手机号码
	 * 
	 * @param mobile
	 * @return [0-9]{5,9}
	 */
	public static boolean isMobileNum(String mobile) {
		boolean flag = false;
		try {
			if(mobile == null){
				return flag;
			}
			// ^1[3|4|5|8][0-9]\d{4,8}$
			// Pattern p =
			// Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0-9]))\\d{8}$");
			Pattern p = Pattern.compile("^1[3|4|5|7|8][0-9]\\d{8}$");
			Matcher m = p.matcher(mobile);
			flag = m.matches();
		} catch (Exception e) {
			flag = false;
		}
		return flag;
	}

	public static String addHtmlColor(String s, String color) {
		if (isBlank(s)) {
			return "";
		}
		String result = "<font color='";
		if (isBlank(color)) {
			color = "red";
		}
		result += color + "'>";
		result += s;
		result += "</font>";
		return result;
	}

	public static boolean isNum(String num) {
		Matcher isNum = numpattern.matcher(num);
		if (isNum.matches()) {
			if(num.length()>10){//如果数字大于10位，大于最大整数，判断为不是数字
				return false;
			}
			return true;
		} else {
			return false;
		}
	}

	public static void main(String[] args) {
		System.out.println(isNum("100"));
	}

	public static String getCell(int row, int col) {
		return getColumnName(col)+row;
	}

	/**
	 * excel 获取一列对应的字母。例如：ColumnNum=1，则返回值为A 列号转字母
	 */
	public static String getColumnName(int col) {
		int first;
		int last;
		String result = "";
		if (col > 256)
			col = 256;
		first = col / 27;
		last = col - (first * 26);

		if (first > 0)
			result = String.valueOf((char) (first + 64));

		if (last > 0)
			result = result + String.valueOf((char) (last + 64));

		return result;
	}
	

	/**
	 * 是否是邮箱
	 * @param email
	 * @return
	 * @author wxy 2015年11月23日星期一中国标准时间上午10:51:23
	 */
	public static boolean isEmail(String email){
		boolean flag = false;
		try {
			if(StringUtils.isBlank(email)){
				return false;
			}
			Pattern p = Pattern.compile("^([a-z0-9A-Z]+[_\\-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$");
			Matcher m = p.matcher(email);
			flag = m.matches();
		} catch (Exception e) {
			flag = false;
		}
		return flag;
	}
	
}
